package restaurant.behaviours.master;

import java.util.Hashtable;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import restaurant.agents.MasterWaiterAgent;
import restaurant.core.CustomizedPerformative;
import restaurant.core.Request;

public class MasterBillBehaviour extends CyclicBehaviour {

	private static final long serialVersionUID = -7017119435790629847L;
	private MasterWaiterAgent agent;

	public MasterBillBehaviour(MasterWaiterAgent agent) {
		this.agent = agent;
	}

	@Override
	public void action() {
		MessageTemplate messageTemplate = MessageTemplate.MatchPerformative(CustomizedPerformative.BILL);
		ACLMessage message = myAgent.receive(messageTemplate);
		if (message != null) {
			// sends the bill
			Request request = Request.parseRequest(message.getContent());
			
			synchronized (this.agent.calledTime) {
				this.agent.calledTime.add(request.getTable(), System.currentTimeMillis());
			}
			
			Hashtable<Request, Boolean> tmp = new Hashtable<Request, Boolean>();
			tmp.put(request, false);
			
			synchronized (this.agent.restaurant.bills) {
				this.agent.restaurant.bills.add(tmp);
			}
		}
	}

}